﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Modeling.Diagrams;
using Microsoft.VisualStudio.Modeling;
using System.Drawing;

using CodeDomHelper.Types;
using Microsoft.RestrictedUsage.CSharp.Semantics;
using devtm.AutoMapper.CustomCode.Helpers;


namespace devtm.AutoMapper
{


    public partial class TypeElementShape
    {

        /// <summary>
        /// Gets the child compartment shape and verifies whether it can resize its parent compartment shape.
        /// </summary>
        /// <value></value>
        /// <returns>true if a child compartment shape can resize its parent compartment shape; otherwise, false.
        /// </returns>
        public override bool AllowsChildrenToResizeParent
        {
            get
            {
                // Overridden to return false so that the visual layout issue is solved, where a shape with a connector that "has custom source"
                // resizes to make the source shape wider until it reaches the edge of the referenced shape.
                // See http://social.msdn.microsoft.com/Forums/en-US/vsx/thread/4cc74f9e-1949-43ba-8407-934f6664167d.
                // It is applied to the base class here so that all inheriting compartment shapes automatically have this fix applied.
                return false;
            }
        }


        public override void OnDoubleClick(DiagramPointEventArgs e)
        {
            base.OnDoubleClick(e);
            
            TypeElement type = this.ModelElement as TypeElement;

            if (type != null)
            {
                var typeExpression = TypeExpression.Analyse(type.AssemblyQualifiedName);

                devtm.Editor.TypeSelector.ParsingSolution.Project prj = Helpers.FindProject(this.Store, typeExpression);

                if (prj != null)
                {

                    var project = prj.Source;

                    if (project != null)
                    {

                        CSharpType item = CSharpParser.SCharpType.Parser.GetType(project, typeExpression.Namespace, typeExpression.Name);

                        if (item != null)
                            item.NavigateToSourceLocation();

                    }
                }
            }
        }


        protected override CompartmentMapping[] GetCompartmentMappings(Type melType)
        {

            CompartmentMapping[] mappings = base.GetCompartmentMappings(melType);

            foreach (ElementListCompartmentMapping mapping in mappings)
                mapping.ImageGetter = CompartmentImageProvider;

            return mappings;

        }


        private Image CompartmentImageProvider(ModelElement element)
        {

            //ItemMap item = element as ItemMap;


            MappingDestination d = element as MappingDestination;
            if (d != null)
                if (d.Mapper == null)
                    return devtm.AutoMapper.Properties.Resources.error;


            MappingSource s = element as MappingSource;
            if (s != null)
                if (s.Mapper == null)
                    return devtm.AutoMapper.Properties.Resources.error;


            return devtm.AutoMapper.Properties.Resources.None;

        }


    }
    
}
